<template>

  <div class="Alphabet-wraper gc-flex gc-flex-col gc-flex-center"  >
    <div class="Alphabet-list"
         v-for="item of letters"
         :key="item"
         :ref="item"
         @click="handleLetterClick"
         @touchstart="handleTouchStart"
         @touchmove="handleTouchMove"
         @touchend="handleTouchEnd"
    >
      {{item}}
    </div>
  </div>

</template>

<script type="es6">
export default {
  name: '',
  props: {
    cities: Object
  },
  data () {
    return {
      touchStatus: false,
      startY: 0,
      timer: null
    }
  },
  computed: {
    letters () {
      const letters = []
      for (let i in this.cities) {
        letters.push(i)
      }
      return letters
    }
  },
  updated () {
    // 当页面发生变化，页面渲染完成
    this.startY = this.$refs['A'][0].offsetTop
  },
  methods: {
    handleLetterClick (e) {
      this.$emit('change', e.target.innerText)
    },
    handleTouchStart () {
      this.touchStatus = true
    },
    handleTouchMove (e) {
      if (this.touchStatus) {
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          const touchY = e.touches[0].clientY - 76
          const index = Math.floor((touchY - this.startY) / 20)
          if (index >= 0 && index < this.letters.length) {
            this.$emit('change', this.letters[index])
          }
        }, 16)
      }
    },
    handleTouchEnd () {
      this.touchStatus = false
    }
  }
}
</script>

<style scoped lang="less">
.Alphabet-wraper{
  position: fixed;
  padding: 0 .12rem;
  right: 0;
  top: 76px;
  height: 88%;
  .Alphabet-list{
    height: 20px;
    line-height: 20px;
    color: #00bcd4;
  }
}
</style>
